//
//  RMoreViewController.m
//  Riji
//
//  Created by  on 2022/8/17.
//

#import "RMoreViewController.h"
#import "RMineCell.h"
#import "RLoginViewController.h"
#import "RPersonViewController.h"

@interface RMoreViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, strong) NSArray *detail;
@property (nonatomic, assign) int removeNum;
@property (nonatomic, assign) int size;
@end

@implementation RMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self loadRemoveNote];
}
- (void)initUI
{
    self.size = (int)[ktool cacheSize];
    NSString *str = [NSString stringWithFormat:@"%dMB",self.size > 0 ? self.size : 0];
    self.title = LS(@"更多设置");
    self.data = notLogin ? @[@[@"清除缓存",@"清空回收站"]] : @[@[@"个人信息"],@[@"清除缓存",@"清空回收站"],@[@"修改登录密码"]];
    self.detail = notLogin ?@[@[str,@"谨慎操作"]] : @[@[@""],@[str,@"谨慎操作"],@[@""]];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
    [self.tableView registerNib:[RMineCell nib] forCellReuseIdentifier:[RMineCell ID]];
}
#pragma mark - 算出回收站有多少日记
- (void)loadRemoveNote{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"remove"),bg_sqlValue(@(YES))];
    [RNoteModel bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        self.removeNum = (int)array.count;
    }];
}
#pragma mark - 退出登录/注销账号
- (void)actionForLogout:(UIButton *)sender
{
    NSString * msg = sender.tag == 200 ? @"注销账号后，账号将不存在，是否继续？" : @"确定退出吗？";
    [self alert:@"提示" msg:msg left:nil right:^{
        if (sender.tag == 200) {
            [CCHttp get:api_delUser para:nil header:nil success:^(id  _Nonnull response) {
                APP.user = nil;
                [RUserModel bg_clear:nil];
                [RNoteModel bg_clear:nil];
                [self.navigationController popToRootViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:notifyNoteUpdate object:nil];
            } fail:^(NSError * _Nonnull error) { }];
        }else{
            APP.user = nil;
            [RUserModel bg_clear:nil];
            RLoginViewController *vc = [RLoginViewController new];
            vc.type = RLoginTypeLogin;
            vc.backBtn.hidden = YES;
            [self.navigationController pushViewController:vc animated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:notifyNoteUpdate object:nil];
        }
    }];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.data[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[RMineCell ID]];
    NSArray *array = self.data[indexPath.section];
    NSString *str = array[indexPath.row];
    cell.type = RMineCellTypeInfo;
    cell.name.text = LS(str);
    cell.line.hidden = indexPath.row == [array count] - 1;
    cell.info.text = LS(self.detail[indexPath.section][indexPath.row]);
    cell.info.textHexColor = [cell.info.text isEqualToString:LS(@"谨慎操作")] ? @"#EE2F2F" : @"#999999";
    cell.backgroundColor = [UIColor whiteColor];
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return section == 2 ? 163 : 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
    if (section == 2 && notLogin == NO) {
        view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 104);
        for (int i = 0; i< 2; i++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 60 + 15 + (15 + 44) * i, SCREEN_WIDTH, 44)];
            btn.tag = 200 + i;
            NSString * t = i == 0 ? LS(@"注销账号") : LS(@"退出登录");
            btn.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
            [btn setTitle:t forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#EE2F2F"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateHighlighted];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            [btn addTarget:self action:@selector(actionForLogout:) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:btn];
        }
        return view;
    }else{
        view.frame = CGRectMake(0, 0, SCREEN_WIDTH, 10);
        return view;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(self)
//    if (notLogin) {
//        return [RLHud msg:LS(@"请先登录")];
//    }
    NSString * str = self.data[indexPath.section][indexPath.row];
    if ([str isEqualToString:@"个人信息"]) {
        [self.navigationController pushViewController:[RPersonViewController new] animated:YES];
    }else if([str isEqualToString:@"清除缓存"]){
        NSString * str = self.size <= 0 ? LS(@"已经很干净啦") : LS(@"清除成功");
        [RLHud msg:str];
    }else if([str isEqualToString:@"清空回收站"]){
        RPopAlertViewController *vc = [RPopAlertViewController new];
        vc.msgLbl.text = self.removeNum <= 0 ? LS(@"回收站是空的") : [NSString stringWithFormat:LS(@"确认要彻底删除回收站的%d条数据吗?"),self.removeNum];
        vc.enterBlock = ^(NSString * _Nonnull string) {
            if (weakself.removeNum > 0) {
                [Tool clearRecycleBin:nil];
            }
        };
        [self presentViewController:vc animated:NO completion:nil];
    }else if ([str isEqualToString:@"修改登录密码"]){
        RLoginViewController *vc = [RLoginViewController new];
        vc.type = RLoginTypeChange;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

@end
